//////////////////////////////////////////////////////////////////
/// Reputation or Interaction:                       		//////
/// What Determines Cooperation on Economic Sanctions? 		//////
/// Dawid Walentek         						  			//////
/// d.walentek@uw.edu.pl									//////
/// Univeristy of Warsaw								    //////
//////////////////////////////////////////////////////////////////

//////////////////////////////
/// Replication do-file //////
//////////////////////////////

/////////////////////////
/// 1 September 2022 ////
/////////////////////////

clear all
set more off

*** path to the data folder
 * cd "here enter your own path to the data folder"

use "Data_Instrumental.dta"

*** path to output
 *cd "here enter your own path to an output folder"

****************************
**** summary statistics ****
****************************

ssc instal outreg2

*** Table 3
outreg2 using Sum_Stat.doc, replace sum(log)

***************************************************************************************************
************************************ Causal Analysis ************************************
***************************************************************************************************

*** control variables
local control us democracy_score_target democracy_score_sender trade security

******************************************
*** logistic regression (main article) ***
******************************************

*** Table 4

logit multilateral past_commit sqrt_numyio, r
 outreg2 using logit_reg.doc, replace ctitle(Model 1) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numyio, r
 outreg2 using logit_reg.doc, append ctitle(Model 2) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numyio `control', r
 outreg2 using logit_reg.doc, append ctitle(Model 3) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit ln_numyrs, r
 outreg2 using logit_reg.doc, append ctitle(Model 4) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.ln_numyrs, r
 outreg2 using logit_reg.doc, append ctitle(Model 5) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.ln_numyrs `control', r
 outreg2 using logit_reg.doc, append ctitle(Model 6) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit sqrt_numigo, r
 outreg2 using logit_reg.doc, append ctitle(Model 7) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numigo, r
 outreg2 using logit_reg.doc, append ctitle(Model 8) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numigo `control', r
 outreg2 using logit_reg.doc, append ctitle(Model 9) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))

*** margins
ssc install grstyle

grstyle init
grstyle set plain, grid
grstyle set color cblind 

label define past_commit_label 1 "Weak" 2 "Moderate" 3 "High"
label values past_commit past_commit_label

* source: https://econpapers.repec.org/software/bocbocode/s458145.htm
ssc install marhis

* Model 2 in Table 4
* Figure 1 Panel (a) 
 logit multilateral i.past_commit##c.sqrt_numyio, r
marhis sqrt_numyio, cate(past_commit)

* Model 5 in Table 4
* Figure 1 Panel (a) 
logit multilateral i.past_commit##c.ln_numyrs, r
marhis ln_numyrs, cate(past_commit)

* Model 8 in Table 4
* Figure 1 Panel (c) 
logit multilateral i.past_commit##c.sqrt_numigo, r
marhis sqrt_numigo, cate(past_commit)

******************************************
*** robustness tests (online appendix) ***
******************************************


*** logistic regression excluding aid withdraw
* Table A.1

logit multilateral past_commit sqrt_numyio if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, replace ctitle(Model 1) eform addtext (Interaction term, NO, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numyio if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 2) eform addtext (Interaction term, YES, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numyio `control' if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 3) eform addtext (Interaction term, YES, Control variables, YES, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit ln_numyrs if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 4) eform addtext (Interaction term, NO, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.ln_numyrs if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 5) eform addtext (Interaction term, YES, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.ln_numyrs `control' if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 6) eform addtext (Interaction term, YES, Control variables, YES, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit sqrt_numigo if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 7) eform addtext (Interaction term, NO, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numigo if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 8) eform addtext (Interaction term, YES, Control variables, NO, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.sqrt_numigo `control' if aid_withdraw==0, r
 outreg2 using logit_aid_reg.doc, append ctitle(Model 9) eform addtext (Interaction term, YES, Control variables, YES, Aid withdraw, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))

*** logistic regression no data transformation
* Table A.2

logit multilateral past_commit numyio, r
 outreg2 using logit_trans_reg.doc, replace ctitle(Model 1) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numyio, r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 2) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numyio `control', r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 3) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit numyrs, r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 4) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numyrs, r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 5) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numyrs `control', r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 6) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
 
logit multilateral past_commit numigo, r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 7) eform addtext (Interaction term, NO, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numigo, r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 8) eform addtext (Interaction term, YES, Control variables, NO) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))
logit multilateral c.past_commit##c.numigo `control', r
 outreg2 using logit_trans_reg.doc, append ctitle(Model 9) eform addtext (Interaction term, YES, Control variables, YES) addstat(Pseudo R2, e(r2_p), Log Lik, e(ll))

*** OLS regression
* Table A.3

reg multilateral past_commit sqrt_numyio, r
 outreg2 using ols_reg.doc, replace ctitle(Model 1) eform addtext (Interaction term, NO, Control variables, NO)
reg multilateral c.past_commit##c.sqrt_numyio, r
 outreg2 using ols_reg.doc, append ctitle(Model 2) eform addtext (Interaction term, YES, Control variables, NO)
reg multilateral c.past_commit##c.sqrt_numyio `control', r
 outreg2 using ols_reg.doc, append ctitle(Model 3) eform addtext (Interaction term, YES, Control variables, YES)
 
reg multilateral past_commit ln_numyrs, r
 outreg2 using ols_reg.doc, append ctitle(Model 4) eform addtext (Interaction term, NO, Control variables, NO)
reg multilateral c.past_commit##c.ln_numyrs, r
 outreg2 using ols_reg.doc, append ctitle(Model 5) eform addtext (Interaction term, YES, Control variables, NO)
reg multilateral c.past_commit##c.ln_numyrs `control', r
 outreg2 using ols_reg.doc, append ctitle(Model 6) eform addtext (Interaction term, YES, Control variables, YES)
 
reg multilateral past_commit sqrt_numigo, r
 outreg2 using ols_reg.doc, append ctitle(Model 7) eform addtext (Interaction term, NO, Control variables, NO) 
reg multilateral c.past_commit##c.sqrt_numigo, r
 outreg2 using ols_reg.doc, append ctitle(Model 8) eform addtext (Interaction term, YES, Control variables, NO)
reg multilateral c.past_commit##c.sqrt_numigo `control', r
 outreg2 using ols_reg.doc, append ctitle(Model 9) eform addtext (Interaction term, YES, Control variables, YES)

*** Margins

* Model 2 in Table A.3
reg multilateral i.past_commit##c.sqrt_numyio, r
marhis sqrt_numyio, cate(past_commit)

* Model 5 in Table A.3
reg multilateral i.past_commit##c.ln_numyrs, r
marhis ln_numyrs, cate(past_commit)

* Model 8 in Table A.3
reg multilateral i.past_commit##c.sqrt_numigo, r
marhis sqrt_numigo, cate(past_commit)

******************************
**** sensitivity analysis ****
******************************

* based on logistic regression in Table 4 (Model (4))

* Plot of ROC Curve using LROC
* Figure A.1 
logit multilateral i.past_commit##c.sqrt_numyio `control', r
predict xb, xb
lroc, graphregion(color(white))
graph export instrumental_lroc.pdf, replace

 
* Plot of Y=Standardized Residual versus X=Observation Number
* Figure A.1
logit multilateral i.past_commit##c.sqrt_numyio `control', r
predict std_residual, rs
label variable std_residual "Standardized Residual"
drop if std_residual==.
generate index=_n
label variable index "Observation Number"
graph twoway (scatter std_residual index,msymbol(d)), xlabel(0(10)160) ylabel(-4(2)4) title("Plot of Standardized Residuals versus Observation Number") xtitle("Observation Number") ytitle("Standardized Residual") yline(0) graphregion(color(white))
graph export instrumental_stdres.pdf, replace

* Plot of Influential Observations (Cook distance)
* Figure A.1
logit multilateral i.past_commit##c.sqrt_numyio `control', r
predict cook, dbeta
label variable cook "Cook Distance"
drop if cook==.
graph twoway (scatter cook index, msymbol(d)), xlabel (0(10)160) title("Plot of Cook Distance versus Observation Number") xtitle("Observation Number") ytitle("Cook Distance") graphregion(color(white))
graph export instrumental_cook.pdf, replace


